package com.kaiwukj.android.ufamily.mvp.ui.page.keeper;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseMvpActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.di.component.DaggerKeeperComponent;
import com.kaiwukj.android.ufamily.di.module.KeeperModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;
import com.kaiwukj.android.ufamily.mvp.ui.widget.RefreshLayout;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.KeeperOrderHistoryUrl)
public class KeeperOrderHistoryActivity extends BaseMvpActivity<KeeperPresenter> implements KeeperContract.View {

    private static final int REQ_NEW_ORDER = 10;//再来一单

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.empty_view)
    QMUIEmptyView emptyView;

    private int page = 1;
    private int pageSize = 12;
    private boolean isLoadingMore = false;

    private KeeperOrderHistoryAdapter mListAdapter;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerKeeperComponent.builder()
                .appComponent(appComponent)
                .keeperModule(new KeeperModule(this))
                .build().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list_normal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("历史记录");
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(15));
        mListAdapter = new KeeperOrderHistoryAdapter();
        rvList.setAdapter(mListAdapter);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout) {
                isLoadingMore = true;
                mPresenter.fetchKeeperOrderHistory(++page, pageSize);
            }

            @Override
            public void onRefresh(@NonNull com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout) {
                mPresenter.fetchKeeperOrderHistory(page = 1, pageSize);
            }
        });

        mListAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            KeeperOrderResult itemResult = mListAdapter.getItem(position);
            if (itemResult == null) return;
            switch (view.getId()) {
                case R.id.btn_order_report:
                    ARouter.getInstance().build(ARouterUrlKt.KeeperReportUrl)
                            .withInt("keeperId", itemResult.getHousekeeperAccountId())
                            .navigation();
                    break;
                case R.id.btn_order_again:
                    ARouter.getInstance().build(ARouterUrlKt.KeeperOrderCreateUrl)
                            .withInt("keeperId", itemResult.getHousekeeperAccountId())
                            .withInt("serviceId", itemResult.getHousekeeperServiceId())
                            .withString("serviceName", itemResult.getServiceName())
                            .navigation(this, REQ_NEW_ORDER);
                    break;
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mPresenter.fetchKeeperOrderHistory(page, pageSize);
        emptyView.show(true);

    }

    @Override
    public void showKeeperInfo(KeeperResult result) {

    }

    @Override
    public void showKeeperList(List<KeeperResult> results) {

    }

    @Override
    public void showKeeperOderList(List<KeeperOrderResult> results) {
        emptyView.hide();
        if (isLoadingMore) {
            isLoadingMore = false;
            mListAdapter.addData(results);
            if (results.size() < pageSize) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore(true);
            }
        } else {
            mListAdapter.setList(results);
            refreshLayout.finishRefresh(true);
        }
        if (mListAdapter.getItemCount() <= 0) {
            emptyView.show("暂没有订单记录", "");
        }
    }

    @Override
    public void onTaskErr(int taskId) {
        super.onTaskErr(taskId);
        emptyView.show("请求失败", "");
        refreshLayout.finishRefresh(false);
        refreshLayout.finishLoadMore(false);
        isLoadingMore = false;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_NEW_ORDER) {
            if (resultCode == RESULT_OK) {
                refreshLayout.autoRefresh();
            }
        }
    }
}
